package gui.partb;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class Q3 {
    public static void main(String[] args) {
        // 打印提示信息
        printInfo();
        // 获取功能代号
        int code = doOperation();
        // 根据输入代号执行功能
        goOption(code);
    }

    // 存储文件路径
    private static final String STUDENTS_URL = "D:/students.dat";
    private static final String COURSES_URL = "D:/courses.dat";
    private static final String TEACHERS_URL = "D:/teachers.dat";
    private static final String GRADES_URL = "D:/grades.dat";

    // 具体执行功能
    private static void goOption(int code) {
        switch (code) {
            case 0:
                System.exit(0);
                break;
            case 1:
                List<Object> students = new ArrayList<>();
                // 文件中已有的数据
                readFile(students, STUDENTS_URL);
                while (true) {
                    System.out.print("Please input student information (input '-1' to end):\n");
                    // 学生id
                    System.out.print("input student Id:");
                    Scanner scanId = new Scanner(System.in);
                    String studentId = scanId.nextLine();
                    if ("-1".equals(studentId)) {
                        DataAccess.saveToFile(students, STUDENTS_URL);
                        redOpt();
                        break;
                    }
                    // 学生信息
                    System.out.print("input student name:");
                    Scanner scanName = new Scanner(System.in);
                    String studentName = scanName.nextLine();
                    System.out.print("input student gender (M/F):");
                    Scanner scanGender = new Scanner(System.in);
                    String studentGender = scanGender.nextLine();
                    if (studentGender.contains("M") || studentGender.contains("F")) {
                        Student student = new Student(studentId, studentName, studentGender);
                        // 避免id重复
                        boolean flag = true;
                        for (Object o : students) {
                            Student s = (Student) o;
                            if (studentId.equals(s.getStuId())) {
                                flag = false;
                                System.out.println("The current ID already exists!");
                                break;
                            }
                        }
                        if (flag) {
                            students.add(student);
                        }
                    } else {
                        System.out.println("input student gender error!");
                    }

                }
                break;
            case 2:
                List<Object> courses = new ArrayList<>();
                readFile(courses, COURSES_URL);
                while (true) {
                    System.out.print("Please input course information (input '-1' to end):\n");
                    // 课程id
                    System.out.print("input course courseId:");
                    Scanner scanId = new Scanner(System.in);
                    String courseId = scanId.nextLine();
                    if ("-1".equals(courseId)) {
                        DataAccess.saveToFile(courses, COURSES_URL);
                        redOpt();
                        break;
                    }
                    // 课程信息
                    System.out.print("input course courseName:");
                    Scanner scanName = new Scanner(System.in);
                    String courseName = scanName.nextLine();
                    Course course = new Course(courseId, courseName);
                    // 避免id重复
                    boolean flag = true;
                    for (Object o : courses) {
                        Course s = (Course) o;
                        if (courseId.equals(s.getCourseId())) {
                            flag = false;
                            System.out.println("The current ID already exists!");
                            break;
                        }
                    }
                    if (flag) {
                        courses.add(course);
                    }
                }
                break;
            case 3:
                List<Object> teachers = new ArrayList<>();
                readFile(teachers, TEACHERS_URL);
                while (true) {
                    System.out.print("Please input teacher information (input '-1' to end):\n");
                    // 教师id
                    System.out.print("input teacher teacherId:");
                    Scanner scanId = new Scanner(System.in);
                    String teacherId = scanId.nextLine();
                    if ("-1".equals(teacherId)) {
                        DataAccess.saveToFile(teachers, TEACHERS_URL);
                        redOpt();
                        break;
                    }
                    // 教师信息
                    System.out.print("input teacher name:");
                    Scanner scanName = new Scanner(System.in);
                    String teacherName = scanName.nextLine();
                    System.out.print("input teacher gender (M/F):");
                    Scanner scanGender = new Scanner(System.in);
                    String teacherGender = scanGender.nextLine();
                    if (teacherGender.contains("M") || teacherGender.contains("F")) {
                        System.out.print("input teacher loginName:");
                        Scanner scanLoginName = new Scanner(System.in);
                        String loginName = scanLoginName.nextLine();
                        System.out.print("input teacher password:");
                        Scanner scanPassword = new Scanner(System.in);
                        String password = scanPassword.nextLine();
                        // 密码加密
                        String encrypt = null;
                        try {
                            encrypt = Encryption.encrypt(password);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Teacher teacher = new Teacher(teacherName, teacherGender, teacherId, loginName, encrypt);
                        // 避免id重复
                        boolean flag = true;
                        for (Object o : teachers) {
                            Teacher s = (Teacher) o;
                            if (teacherId.equals(s.getTeacherId())) {
                                flag = false;
                                System.out.println("The current ID already exists!");
                                break;
                            }
                        }
                        if (flag) {
                            teachers.add(teacher);
                        }
                    } else {
                        System.out.println("input teacher gender error!");
                    }
                }
                break;
            case 4:
                List<Object> grades = new ArrayList<>();
                readFile(grades, GRADES_URL);
                while (true) {
                    System.out.print("Please input grade information (input '-1' to end):\n");
                    // 班级id
                    System.out.print("input grade grade:");
                    Scanner scanId = new Scanner(System.in);
                    String gradeInfo = scanId.nextLine();
                    if ("-1".equals(gradeInfo)) {
                        DataAccess.saveToFile(grades, GRADES_URL);
                        redOpt();
                        break;
                    }
                    // 班级信息
                    System.out.print("input grade stuId:");
                    Scanner scanName = new Scanner(System.in);
                    String stuId = scanName.nextLine();
                    System.out.print("input grade courseId:");
                    Scanner scanGender = new Scanner(System.in);
                    String courseId = scanGender.nextLine();
                    Grade grade = new Grade(gradeInfo, stuId, courseId);
                    // 避免id重复
                    boolean flag = true;
                    for (Object o : grades) {
                        Grade s = (Grade) o;
                        if (gradeInfo.equals(s.getGrade())) {
                            flag = false;
                            System.out.println("The current ID already exists!");
                            break;
                        }
                    }
                    if (flag) {
                        grades.add(grade);
                    }
                }
                break;
            case 5:
                List<?> studentO = (List<?>) FileUtils.readObjectFromFile(STUDENTS_URL);
                if (studentO != null && studentO.size() > 0) {
                    StringBuilder sb = new StringBuilder("[");
                    for (int i = 0; i < studentO.size(); i++) {
                        Student s = (Student) studentO.get(i);
                        if (i == studentO.size() - 1) {
                            sb.append(s.getStuId()).append(" ").append(s.getName()).append(" ").append(s.getGender()).append("]");
                        } else {
                            sb.append(s.getStuId()).append(" ").append(s.getName()).append(" ").append(s.getGender()).append(", ");
                        }
                    }
                    System.out.println(sb);
                } else {
                    System.out.println("There is no student.");
                }
                redOpt();
                break;
            case 6:
                List<?> coursesO = (List<?>) FileUtils.readObjectFromFile(COURSES_URL);
                if (coursesO != null && coursesO.size() > 0) {
                    StringBuilder sb = new StringBuilder("[");
                    for (int i = 0; i < coursesO.size(); i++) {
                        Course s = (Course) coursesO.get(i);
                        if (i == coursesO.size() - 1) {
                            sb.append(s.getCourseId()).append(" ").append(s.getCourseName()).append("]");
                        } else {
                            sb.append(s.getCourseId()).append(" ").append(s.getCourseName()).append(", ");
                        }
                    }
                    System.out.println(sb);
                } else {
                    System.out.println("There is no courses.");
                }
                redOpt();
                break;
            case 7:
                List<?> teacherO = (List<?>) FileUtils.readObjectFromFile(TEACHERS_URL);
                if (teacherO != null && teacherO.size() > 0) {
                    StringBuilder sb = new StringBuilder("[");
                    for (int i = 0; i < teacherO.size(); i++) {
                        Teacher s = (Teacher) teacherO.get(i);
                        // 密码解密
                        String decrypt = null;
                        try {
                            decrypt = Encryption.decrypt(s.getPassword());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (i == teacherO.size() - 1) {
                            sb.append(s.getTeacherId()).append(" ").append(s.getName()).append(" ").append(s.getGender())
                                    .append(" ").append(s.getLoginName()).append(" ").append(decrypt).append("]");
                        } else {
                            sb.append(s.getTeacherId()).append(" ").append(s.getName()).append(" ").append(s.getGender())
                                    .append(" ").append(s.getLoginName()).append(" ").append(s.getPassword()).append(", ");
                        }
                    }
                    System.out.println(sb);
                } else {
                    System.out.println("There is no teacher.");
                }
                redOpt();
                break;
            case 8:
                List<?> gradeO = (List<?>) FileUtils.readObjectFromFile(GRADES_URL);
                if (gradeO != null && gradeO.size() > 0) {
                    StringBuilder sb = new StringBuilder("[");
                    for (int i = 0; i < gradeO.size(); i++) {
                        Grade s = (Grade) gradeO.get(i);
                        if (i == gradeO.size() - 1) {
                            sb.append(s.getGrade()).append(" ").append(s.getStuId()).append(" ").append(s.getCourseId()).append("]");
                        } else {
                            sb.append(s.getGrade()).append(" ").append(s.getStuId()).append(" ").append(s.getCourseId()).append(", ");
                        }
                    }
                    System.out.println(sb);
                } else {
                    System.out.println("There is no grade.");
                }
                redOpt();
                break;
            case 9:
                System.out.print("Input student id for search:");
                Scanner scanId = new Scanner(System.in);
                String studentId = scanId.nextLine();
                // 学生id查询
                DataAccess.getStudentById(studentId, STUDENTS_URL);
                redOpt();
                break;
            case 10:
                System.out.print("Input course id for search:");
                Scanner courseScan = new Scanner(System.in);
                String courseId = courseScan.nextLine();
                // 班级id查询
                DataAccess.getCourseById(courseId, COURSES_URL);
                redOpt();
                break;
            case 11:
                System.out.print("Input teacher id for search:");
                Scanner teacherScan = new Scanner(System.in);
                String teacherId = teacherScan.nextLine();
                // 教师id查询
                DataAccess.getTeacherById(teacherId, TEACHERS_URL);
                redOpt();
                break;
            case 12:
                System.out.print("Input grade grade for search:");
                Scanner gradeScan = new Scanner(System.in);
                String grade = gradeScan.nextLine();
                // 班级id查询
                DataAccess.getGradeById(grade, GRADES_URL);
                redOpt();
                break;
            default:
                System.out.println("default");
        }
    }

    // 读取指定的文件
    private static void readFile(List<Object> courses, String url) {
        // 文件中已有的数据
        List<?> o = (List<?>) FileUtils.readObjectFromFile(url);
        if (o != null && o.size() > 0) {
            courses.addAll(o);
        }
    }

    // 任意键继续
    private static void redOpt() {
        try {
            System.out.print("press any key to continue....");
            int read = System.in.read();
        } catch (IOException e) {
            // e.printStackTrace();
        }
        int i1 = doOperation();
        goOption(i1);
    }

    // 功能列表打印
    public static void printInfo() {
        System.out.println("" +
                "***** Operation Menu *****\n" +
                "1:add a student.\n" +
                "2:add a course.\n" +
                "3:add a teacher.\n" +
                "4:add a grade.\n" +
                "5:list all students.\n" +
                "6:list all courses.\n" +
                "7:list all teachers.\n" +
                "8:list all grades.\n" +
                "9:search a student.\n" +
                "10:search a course.\n" +
                "11:search a teacher.\n" +
                "12:search a grade.\n" +
                "0:exit the program." +
                "");
    }

    // 输入具体功能号
    private static int doOperation() {
        System.out.print("Please input a number to run the program:");
        while (true) {
            try {
                Scanner scanner = new Scanner(System.in);
                int code = scanner.nextInt();
                if (code >= 0 && code <= 12) {
                    return code;
                } else {
                    System.out.print("Please input a number to run the program:");
                }
            } catch (Exception e) {
                System.out.print("Please input a number to run the program:");
            }
        }
    }
}
